﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TheBox
{
    class Player
    {
        public static int Height = 8;
        public static int Width = 15;
        public Vector2 Speed = new Vector2(0,0);
        public Vector2 Pos;
        public Box BaseBox;
        public Texture2D Texture;
        public Rectangle Rect;
        public int StartX;
        public static int StartY = 420;
        public float Rotation = 0.0f;
        public static Vector2 Origin = new Vector2(7,4);
        public int Lives = 5;
        public int Score = 0;

        public Keys UpKey;
        public Keys DownKey;
        public Keys LeftKey;
        public Keys RightKey;
        public Keys FireKey;

        public float CurrentSleepBeforeNextShot = 0.0f;

        private static int heartWidth = 25;
        private static int heartHeight = 25;
        private static int heartSpaceWidth = 5;
        private Texture2D heartTexture;
        public static float RotationSpeed = 6;
        private SpriteFont font;


        public void DrawInfo(SpriteBatch spriteBatch)
        {
            int curY = StartY;
            int curX = StartX;
            for (int i = 0; i < Lives; i++)
            {
                spriteBatch.Draw(heartTexture,
                    new Rectangle(curX, curY, heartWidth, heartHeight),
                    Color.White);
                curX += heartWidth + heartSpaceWidth;
            }

            curY += 30;
            curX = StartX;
            spriteBatch.DrawString(font, "Score: " + Score.ToString(), new Vector2(curX, curY), Color.Green);

        }

        public Boolean IsDead()
        {
            return (Lives <= 0);
        }

        public void Hit()
        {
            Lives--;
        }

        public Player(SpriteFont font, Box baseBox, Texture2D texture,int startX,Texture2D heartTexture,
            Keys UpKey, Keys DownKey, Keys LeftKey, Keys RightKey, Keys FireKey)
        {
            BaseBox = baseBox;
            Texture = texture;
            Pos = Box.Origin;
            this.StartX = startX;
            this.heartTexture = heartTexture;
            this.UpKey = UpKey;
            this.DownKey = DownKey;
            this.LeftKey = LeftKey;
            this.RightKey = RightKey;
            this.FireKey = FireKey;
            this.font = font;
        }

        public void addSpeed(Vector2 direction)
        {
            Matrix rotationMatrix = Matrix.CreateRotationZ(-1 * BaseBox.Rotation + Rotation);
            Speed += Vector2.Transform(direction * 400, rotationMatrix);
        }

        public void update(GameTime gameTime)
        {
            Matrix rotationMatrix;

            Pos += Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            rotationMatrix = Matrix.CreateRotationZ(BaseBox.Rotation);
            Vector2 newPos = Vector2.Transform(Pos - Box.Origin, rotationMatrix) + BaseBox.Position;
            Rect = new Rectangle((int)newPos.X - Width / 2, (int)newPos.Y - Height / 2, Width, Height);

        }

        internal void AddScore()
        {
            Score += 100;
        }
    }
}
